<%@page import="com.winexpress.util.SessionAttributes"%>
<%@page import="com.winexpress.product.Wine"%>
<%@page import="java.util.List"%>
<%@page import="com.winexpress.product.Category"%>
<%@page import="java.util.ArrayList"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>wineXpress Edit Wine</title>
    </head>
    <body>
        <t:adminNavigation/>

        <div class="header">
            <div class="logo_img"><img alt="Site Name" src="/WineXpressClient/resources/images/logo.png"></div>
        </div>

        <div class="editWine">
            <div class="panel panel-default" style="width:70%; margin-left: 15%; margin-top: 10px" >
                <div class="panel-heading">
                    <h3 class="panel-title">Edit Wine</h3>
                </div>
                <% 
                    Wine w = (Wine) request.getAttribute("wine");
                    Category wineCategory = (Category) request.getAttribute("category");
                %>
                <div class="panel-body">
                    <form class="form-horizontal"  id="defaultForm"  name="editWine" action="/WineXpressClient/wineAddEditHandler/editWines" enctype="multipart/form-data" method="post">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Name</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="name" value="<%=w.getName()%>"> 
                                <input name="wineID" id="wineID" type="hidden" value=<%=w.getId()%> />
                                <input name="oldCategoryID" id="oldCategoryID" type="hidden" value=<%=wineCategory.getCategoryID()%> />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Description</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="description" value="<%=w.getDescription()%>">
                            </div>
                        </div>
                        <div class="form-group" >
                            <%  List<Category> list = (List) request.getAttribute("categoryList");%>
                            <label class="col-sm-2 control-label">Category</label>   
                            <div class="col-sm-10">
                                <select class="form-control" name="category">
                                    <option value="">-- Select a category --</option>
                                    <% if (list != null && !list.isEmpty()) {
                                            for (Object o : list) {
                                                Category c = (Category) o;
                                                if (!c.getCategoryID().equals(wineCategory.getCategoryID())) {%>
                                    <option value="<%=c.getCategoryID()%>"> <%=c.getName()%> </option> 
                                    <%
                                    } else {%>
                                    <option value="<%=c.getCategoryID()%>" selected="selected" > <%=c.getName()%> </option>   
                                    <% } %>
                                    <%}
                                    } else {%>
                                    <tr>No Category in the database.</tr>
                                    <%}%>
                                </select>
                            </div>               
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Producer</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="producer" value="<%=w.getProducer()%>" >
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Country</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="country" value="<%=w.getCountry()%>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Region</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="region" value="<%=w.getRegion()%>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Varietal</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="varietal" value="<%=w.getVarietal()%>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Vintage</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="vintage" value="<%=w.getVintage()%>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Stock</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="stock" value="<%=w.getStock()%>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Volume (mL)</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="volume" value="<%=w.getVolume()%>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Price (SGD)</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="price" value="<%=w.getPrice()%>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Cost (SGD)</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="cost" value="<%=w.getCost()%>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Discount (%)</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="discount" value="<%=w.getDiscount()%>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Change Image</label>
                            <div class="col-sm-10">
                                <img src="<%=w.getImageUrl()%>" style="height: 20%; width: 20%;margin: auto; alignment-adjust: central">
                                <input type="file" name="file"/>
                            </div>
                        </div> 
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-default">Submit</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

        </div>

        <script type="text/javascript">
            $(document).ready(function() {
                $('#defaultForm').bootstrapValidator({
                    message: 'This value is not valid',
                    fields: {
                        category: {
                          validators: {
                              notEmpty: {
                                    message: 'Category is required and can\'t be empty'
                                },
                            }  
                        },
                        stock: {
                            validators: {
                                notEmpty: {
                                    message: 'Stock is required and can\'t be empty'
                                },
                            }
                        },
                        volume: {
                            validators: {
                                notEmpty: {
                                    message: 'Volume is required and can\'t be empty'
                                },
                                greaterThan: {
                                    value: 0,
                                    inclusive: false,
                                    message: 'Volume has to be greater than 0'
                                },
                                digits: {
                                    message: 'The value can contain only digits'
                                }
                            }
                        },
                        price: {
                            validators: {
                                notEmpty: {
                                    message: 'Price is required and can\'t be empty'
                                },
                                greaterThan: {
                                    value: 0,
                                    inclusive: false,
                                    message: 'Price has to be greater than 0'
                                }
                            }
                        },
                        cost: {
                            validators: {
                                notEmpty: {
                                    message: 'Cost is required and can\'t be empty'
                                },
                                greaterThan: {
                                    value: 0,
                                    inclusive: true,
                                    message: 'Cost has to be greater than 0'
                                }
                            }
                        },
                        discount: {
                            validators: {
                                notEmpty: {
                                    message: 'Discount is required and can\'t be empty'
                                },
                                lessThan: {
                                    value: 100,
                                    inclusive: true,
                                    message: 'Discount has to be less than or equals to 100'
                                },
                                greaterThan: {
                                    value: 0,
                                    inclusive: false,
                                    message: 'Discount has to be greater than or equals to 0'
                                }
                            }
                        }
                    }
                })
            })
        </script>

    </body>
</html>
